package javbot.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class CommandLineExecutor {
	
	public static boolean outputToSysout = false;
	public static File outputToFile = null;
	
	
	public static void cmdExecute(String cmdline) {
		try {
			Process p = Runtime.getRuntime().exec(cmdline);
			BufferedReader input = new BufferedReader(
					new InputStreamReader(p.getInputStream()));
			
						
			String line;
			FileWriter fw = null;
			
			if (outputToFile != null)
				fw = new FileWriter(outputToFile);
			
			while ((line = input.readLine()) != null) {
				if (outputToSysout)
					System.out.println(line);
				if (fw != null)
					fw.write(line + "\n");	
			}
			input.close();
			
			if (fw!=null) fw.close();
			
		} catch (Exception err) {
			err.printStackTrace();
		}
	}	
}
